package com.study.controller;

import com.study.common.Result;
import com.study.common.ResultCode;
import com.study.dto.PermissionDto;
import com.study.entity.system.Permission;
import com.study.exception.CommonException;
import com.study.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wfw
 * @Date 2020/07/17 10:34
 */
@CrossOrigin
@RestController
@RequestMapping("sys/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    @PostMapping("")
    public Result save(@RequestBody PermissionDto permissionDto) {
        permissionService.save(permissionDto);
        return new Result(ResultCode.SUCCESS);
    }

    @PutMapping("{id}")
    public Result update(@PathVariable(value = "id") String id, @RequestBody PermissionDto permissionDto) {
        //构造id
        permissionDto.setId(id);
        permissionService.update(permissionDto);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("")
    public Result findAll(@RequestParam() Permission param) {
        List<Permission> list = permissionService.findAll(param);
        return new Result(ResultCode.SUCCESS, list);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable(value = "id") String id) throws CommonException {
        PermissionDto permissionDto = permissionService.findById(id);
        return new Result(ResultCode.SUCCESS, permissionDto);
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable(value = "id") String id) throws CommonException {
        permissionService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    @GetMapping("getMenus")
    public Result getMenus() throws CommonException {
        List<Permission> menus = permissionService.getMenus();
        return new Result(ResultCode.SUCCESS, menus);
    }
}
